import os
import sys
from multiprocessing import Process


def run_django(path):
    django_manage_path = os.path.join(path, 'SpiderDjango')
    disc = django_manage_path.split(':')[0]
    try:
        os.system(f'cd {django_manage_path}&{disc}:&python manage.py runserver')
    except:
        print('django 关闭')


def create_chrome(path):
    chrome_path, chrome = path.rsplit('/', 1)
    disc = path.split(':')[0]
    os.system(f'cd {chrome_path}&{disc}:&{chrome} --remote-debugging-port=9222 --start-maximized')


if __name__ == '__main__':
    BASE_PATH = os.path.dirname(os.path.abspath(__file__))
    spider_project_path = os.path.join(BASE_PATH, 'SpiderProject')
    sys.path.insert(0, spider_project_path)
    from ProxiesPool.start import RunProxyPool
    from Spider.RunSpider import get_run
    from Spider.settings import PROXY_ENABLED, PROXY_TYPE, CHROME_PATH
    if PROXY_ENABLED and (PROXY_TYPE == 'POOL'):
        RunProxyPool()
    Process(target=run_django, args=(BASE_PATH,)).start()
    Process(target=create_chrome, args=(CHROME_PATH,)).start()
    SpiderRun = get_run()
    try:
        Process(target=SpiderRun).run()
    except KeyboardInterrupt:
        pass
